package com.anche.cywebnew.Utils.Thread;

import java.util.concurrent.*;

/**
 * @program: cywebnew
 * @description: 项目公共线程池
 * @author: LCQ
 * @create: 2021-01-21 09:34
 */
public class ThreadPoolUtil {
    public static ThreadPoolExecutor threadPool;

    /**
     * 无返回值直接执行
     * @param runnable
     */
    public  static void execute(Runnable runnable){
        getThreadPool().execute(runnable);
    }

    /**
     * 返回值直接执行
     * @param callable
     */
    public  static <T> Future<T> submit(Callable<T> callable){
        return   getThreadPool().submit(callable);
    }

    /**
     * 关闭线程池
     */
//    public static void shutdown() {
//        getThreadPool().shutdown();
//    }

    /**
     * dcs获取线程池
     * @return 线程池对象
     */
    public static ThreadPoolExecutor getThreadPool() {
        if (threadPool != null) {
            return threadPool;
        } else {
            synchronized (ThreadPoolUtil.class) {
                if (threadPool == null) {
                    threadPool = new ThreadPoolExecutor(10, 50, 60, TimeUnit.SECONDS,
                            new LinkedBlockingQueue<>(100), new ThreadPoolExecutor.CallerRunsPolicy());
                }
                return threadPool;
            }
        }
    }
}
